package com.myl.service.system.impl;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.ibatis.session.SqlSession;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myl.dao.system.ModuleDao;
import com.myl.domain.system.Module;
import com.myl.factory.MapperFactory;
import com.myl.service.system.ModuleService;
import com.myl.utils.MD5Util;
import com.myl.utils.TransactionUtil;

public class ModuleServiceImp implements ModuleService{

	@Override
	public void save(Module module) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);
			//id使用uuid的生成策略来获取
			String id =UUID.randomUUID().toString();
			module.setId(id);
			//使用md5加密
			//module.setPassword(MD5Util.md5(module.getPassword()));
			
			
			moduleDao.save(module);
			
			TransactionUtil.commit(sqlSession);
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
		
	}

	@Override
	public void delete(Module module) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);		
			//id使用uuid的生成策略来获取
			String id =UUID.randomUUID().toString();
			module.setId(id);
			
			moduleDao.delete(module);
			TransactionUtil.commit(sqlSession);
			
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
	}

	@Override
	public void update(Module module) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);		
			
			//更新方案
			//1.读取数据库的信息，覆盖
			//.修改update的语句
			moduleDao.update(module);
			
			TransactionUtil.commit(sqlSession);
			
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
	}

	@Override
	public Module findById(String id) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);		
			return moduleDao.findById(id);
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
		
	}

	@Override
	public List<Module> findAll() {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);		
			
			List<Module> all = moduleDao.findAll();
			return all;
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
	}

	@Override
	public PageInfo findAll(int page, int size) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);		
			
			PageHelper.startPage(page,size);
			List<Module> all = moduleDao.findAll();
			PageInfo  pageInfo = new PageInfo(all);
			return pageInfo;
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
	}

	@Override
	public List<Map>findAuthorDataByRoleId(String roleId) {
		
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);		
			
			return moduleDao.findAuthorDataByRoleId(roleId);
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
		
	}


}
